package com.infocollect.project.api.helper;

import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.infocollect.project.api.config.TokenService;
import com.infocollect.project.api.vo.CurrentUserInfoVO;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class CurrentHelp {
    public  static  CurrentHelp Ins=new CurrentHelp();
    CurrentHelp(){
    }
    public CurrentUserInfoVO GetUser()  {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = TokenService.Ins.getCookies(request);
        // 执行认证
        if (token == null||token.length()==0) {
            return null ;
        }
        String currentuserJson;
        try {
            currentuserJson = JWT.decode(token).getAudience().get(0);
        } catch (JWTDecodeException j) {
            return null;
        }
        if(currentuserJson!=null&&currentuserJson.length()>0){
            CurrentUserInfoVO currentUserInfo=null;
            currentUserInfo = JSONUtil.jsonToObj(CurrentUserInfoVO.class,currentuserJson);
            if(currentUserInfo==null)
                return null;
            return currentUserInfo;
        }
        return null;
    }
}
